package com.zjl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.jar.Attributes.Name;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LifeServlet
 */
public class LifeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public LifeServlet() {//构造方法
        // TODO Auto-generated constructor stub
    	System.out.println("servlet被实例化");
    }
    //全局变量
    String nameString;
    String pwdString;
    String nameString1;
    @Override
    public void init() throws ServletException {
    	// TODO Auto-generated method stub
    	System.out.println("servlet被初始化");
    	//当前servlet参数
    	ServletConfig servletConfig=this.getServletConfig();
    	 nameString=servletConfig.getInitParameter("name");
    	 pwdString=servletConfig.getInitParameter("pwd");
    	System.out.println(nameString+" "+pwdString);
    	
    	//全局servlet参数
    	ServletContext sContext=this.getServletContext();
    	nameString1=sContext.getInitParameter("name1");
    	//super.init();
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
   
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
    	response.setCharacterEncoding("UTF-8");//设置输出内容的编码格式
		response.setContentType("text/html");//设置输出的文件类型
		PrintWriter out = response.getWriter();//响应页面
		out.println("登录用户:"+nameString+"<br/>");
		out.println("密码:"+pwdString+"<br/>");
		out.println("用户名:"+nameString1);
		out.flush();
		out.close();// 关闭输出流
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
    /*protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}*/

    /*@Override 
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws 
		  ServletException, IOException {    
		    	System.out.println("服务方法 "); 
		    	
		    } */
    @Override 
    public void destroy() {    
    	System.out.println("销毁方法 "); 
    }
    
}
